package com.hngy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hngy.common.EmployeeStatic;
import com.hngy.common.R;
import com.hngy.domain.Employee;
import com.hngy.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author LiKe
 * @date 2021/12/23 15:08
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 后台登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestBody Employee employee){


        //调用service层登录方法
        Employee one = employeeService.login(employee);

        String password = employee.getPassword();

        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //1、判断是否为空
        if (one == null)
            //用户名不存在 登录失败
            return R.error("用户名不存在!登录失败!");

        //2、判断密码是否正确
        if (!one.getPassword().equals(password))
            //密码不相等 登录失败
            return R.error("密码错误！请重新输入");

        //3、判断员工状态是否被禁用
        if (one.getStatus().equals(EmployeeStatic.UNABLE))
            //员工是禁用状态
            return R.error("当前账号已被禁用！");

        //4、登录成功！ 把员工id存到
        request.getSession().setAttribute("employee",one.getId());
        //返回员工
        return R.success(one);

    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        request.getSession().invalidate();
        return R.success("退出成功！");
    }

    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R add(HttpServletRequest request,@RequestBody Employee employee){
        log.info("员工{}",employee.toString());
        //填充员工的信息
        String password = DigestUtils.md5DigestAsHex(("123456").getBytes());
        //设置初始密码
        employee.setPassword(password);
        //创建修改时间 用户
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/

        //获得当前登录用户的id
        Long empId = (Long) request.getSession().getAttribute("employee");

        /*employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/

        employeeService.save(employee);
        return R.success("添加成功！");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}",page,pageSize,name);

        //分页构造器
        IPage<Employee> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 修改员工
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R update(HttpServletRequest request,@RequestBody Employee employee){
        log.info("员工{}",employee.toString());
        Long empId = (Long) request.getSession().getAttribute("employee");
        //更新修改时间和修改人
        /*employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);*/
        log.info("当前线程ID：{}",Thread.currentThread().getId());



        employeeService.updateById(employee);

        return R.success("修改成功！");
    }

    /**
     * 按id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
}
